package com.bazhua.bazo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bazhua.bazo.repository.entity.*;
import com.bazhua.bazo.repository.mapper.ContextServiceMapper;
import com.bazhua.bazo.repository.mapper.CouponCheckRecordMapper;
import com.bazhua.bazo.repository.model.CouponCheckModel;
import com.bazhua.bazo.repository.model.CouponModel;
import com.bazhua.bazo.repository.model.UserType;
import com.bazhua.bazo.repository.model.result.PageListResult;
import com.bazhua.bazo.repository.model.result.ResultUtils;
import com.bazhua.bazo.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: starmile
 * @Description:
 * @Create: 2021-03-02 0:12
 **/
@Service
public class CouponCheckRecordServiceImpl extends ServiceImpl<CouponCheckRecordMapper, CouponCheckRecord> implements ICouponCheckRecordService {

    @Autowired
    private IWechatUserService wechatUserService;

    @Autowired
    private IShopInfoService shopInfoService;

    @Autowired
    private ICouponRecordService couponRecordService;

    private ShopInfo getShopByUserId(Long shopUserId) {
        QueryWrapper<ShopInfo> shopInfoQueryWrapper = new QueryWrapper<>();
        shopInfoQueryWrapper.eq("shop_admin", shopUserId);
        shopInfoQueryWrapper.eq("shop_status", 2);
        ShopInfo shop = shopInfoService.getOne(shopInfoQueryWrapper);
        return shop;
    }

    public void add(CouponCheckRecord couponCheckRecord) {
        baseMapper.insert(couponCheckRecord);
    }

    public PageListResult getPage(Integer pageNum, Integer pageSize, Long userId) {
        QueryWrapper<CouponCheckRecord> queryWrapper = new QueryWrapper();
        ShopInfo shopInfo = getShopByUserId(userId);
        WechatUser wechatUser = wechatUserService.findById(userId);
        if (wechatUser != null && wechatUser.getUserType() == UserType.Shop_Staff) {
            queryWrapper.eq("user_id", wechatUser.getId());
        }
        if (shopInfo == null) {
            return ResultUtils.pageError(90200, "店铺未审核或已禁用");
        }
        queryWrapper.eq("shop_id", shopInfo.getId());
        Page<CouponCheckRecord> page = new Page<>(pageNum, pageSize);
        page = baseMapper.selectPage(page, queryWrapper);
        if (page.getRecords() != null && page.getRecords().size() > 0) {
            Page<CouponCheckModel> modelPage = new Page<>();
            modelPage.setCurrent(page.getCurrent());
            modelPage.setSize(page.getSize());
            modelPage.setTotal(page.getTotal());
            modelPage.setPages(page.getPages());
            modelPage.setMaxLimit(page.getMaxLimit());
            List<CouponCheckModel> list = new ArrayList<>();
            for (CouponCheckRecord couponCheckRecord : page.getRecords()) {
                CouponCheckModel couponCheckModel = new CouponCheckModel();
                WechatUser user = wechatUserService.findById(couponCheckRecord.getUserId());
                if (user == null)
                    continue;
                CouponRecord couponRecord = couponRecordService.getCouponRecord(couponCheckRecord.getCouponRecordId());
                if (couponRecord == null)
                    continue;
                couponCheckModel.setUserName(user.getUserName());
                couponCheckModel.setCreateTime(couponCheckRecord.getCreateTime());
                couponCheckModel.setId(couponCheckRecord.getId());
                couponCheckModel.setCouponNumber(couponRecord.getCouponNumber());
                list.add(couponCheckModel);
            }
            modelPage.setRecords(list);
            return ResultUtils.pageOk(modelPage);
        }
        return ResultUtils.pageError(0, "查询数据为空");
    }
}
